package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.dto.MoctaDto;
import com.example.entity.hz.SFCTA;
import com.example.entity.vo.PgListVo;
import com.example.entity.vo.ProcessVo;
import com.example.entity.vo.pg.PaiGongItemVo;
import com.example.mapper.CmsmwMapper;
import com.example.mapper.MoctyMapper;
import com.example.mapper.SfctaMapper;
import com.example.service.SfctaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-03-18
 */
@Service
public class SfctaServiceImpl extends ServiceImpl<SfctaMapper, SFCTA> implements SfctaService {

    @Resource
    private SfctaMapper sfctaMapper;

    @Resource
    private CmsmwMapper cmsmwMapper;
    @Resource
    private MoctyMapper moctyMapper;

    @Override
    public Result<List<SFCTA>> querySfCtaList(String single, String odd) {
        try {
            QueryWrapper<SFCTA> sfctaQueryWrapper = new QueryWrapper<>();
            sfctaQueryWrapper.eq("TA001", single);
            sfctaQueryWrapper.eq("TA002", odd);
            List<SFCTA> sfctaList = this.sfctaMapper.selectList(sfctaQueryWrapper);
            return Result.success(sfctaList);
        }catch (Exception e){
            throw new YiFeiException(500,"查询工单工艺发生异常"+e.getMessage());
        }
    }

    @Override
    public List<ProcessVo> getProcess() {
        List<ProcessVo> sfctaList = this.sfctaMapper.getProcess();
        return sfctaList;
    }

    @Override
    public List<PgListVo> pgOrderList(MoctaDto moctaDto) {
        List<PaiGongItemVo> list = moctyMapper.getPGListFromMocty(moctaDto);
        List<PgListVo> list1 = moctyMapper.getList(moctaDto);
//        for (PaiGongItemVo paiGongItemVo : list) {
//            List<ProcessVo> processVo = sfctaMapper.getProcess(paiGongItemVo.getWorkSingle(),
//                    paiGongItemVo.getWorkNo());
//            paiGongItemVo.setProcessVos(processVo);
//        }
        List<ProcessVo> processVo = sfctaMapper.getProcessList();
        Map<String, List<ProcessVo>> processVoMap = new HashMap<>();
        for (ProcessVo vo : processVo) {
            // 创建键
            String key = vo.getTA001() + vo.getTA002();
            // 检查 Map 中是否已存在该键
            if (!processVoMap.containsKey(key)) {
                // 如果键不存在，创建一个新的 List 并将其放入 Map 中
                processVoMap.put(key, new ArrayList<>());
            }
            // 将当前的 ProcessVo 对象添加到对应的 List 中
            processVoMap.get(key).add(vo);
        }
        for (PgListVo listVo : list1) {
            List<PaiGongItemVo> pgListVo = new ArrayList<>();
            Double sum = 0.0;
            for (PaiGongItemVo vo : list) {
                if (listVo.getProductId().trim().equals(vo.getProductId().trim())){
                    pgListVo.add(vo);
                    sum+=vo.getWpgNums().doubleValue();
                }
                String key = vo.getWorkSingle() + vo.getWorkNo() ;
                // 查找匹配的 ProcessVo 对象
                if (processVoMap.containsKey(key)) {
                    vo.setProcessVos(processVoMap.get(key));
                }
            }
            listVo.setWpgNums(sum);
            listVo.setPaiGongItemVos(pgListVo);
        }
        return list1;
    }
}
